import type { AxiosResponse } from "axios";
import axios from "./index";
import type {
  LoginRequest,
  LoginResponse,
  RegisterRequest,
  RegisterResponse,
  Result,
  User,
} from "./types";

// 登录
async function login(
  requestData: LoginRequest
): Promise<Result<LoginResponse>> {
  const { data } = await axios.post<Result<LoginResponse>>(
    "/adminUser/admin-login",
    requestData
  );
  return data;
}

async function getUsers(
  page: number,
  limit: number
): Promise<
  Result<{
    users: User[];
    totalCount: number;
  }>
> {
  const { data } = await axios.get<
    Result<{
      users: User[];
      totalCount: number;
    }>
  >("/users", {
    params: {
      page,
      limit,
    },
  });
  return data;
}

// 修改密码
export async function changePassword(
  id: number,
  newPassword: string
): Promise<Result<any>> {
  const { data } = await axios.post<Result<any>>("/adminUser/change-password", {
    newPassword,
    id,
  });
  return data;
}

// 查询用户总数的函数
export async function getTotalUsers(): Promise<number> {
  let { data } = await axios.get("/users/totalUsers");
  return data.data;
}

// 查询过去30天内每天注册的用户数量的函数
export async function getRegistrationsByDay(): Promise<any> {
  let { data } = await axios.get("/users/registrationsByDay");
  return data.data;
}

export { login, getUsers };
